package com.mall.app.web;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mall.app.bean.TbBuyer;
import com.mall.app.service.TbBuyerService;
import com.mall.app.utils.MyAccount;
import com.mall.app.utils.MyToken;


/**
 * <p>
 * 买家表 前端控制器
 * </p>
 *
 * @author LeeSin
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/buyer")
public class TbBuyerController {
	@Autowired
	private TbBuyerService buyerService;
	
	@RequestMapping("/listPage")
	public Map<String,Object> listPage(int page ,int limit,String bname){

		Map<String,Object>  map=new HashMap<String, Object>();
		map.put("page", (page-1)*limit);
		map.put("size", limit);
		map.put("bname", bname);
		Map<String,Object>  result=new HashMap<String, Object>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("count", buyerService.getCount(map));
		result.put("data", buyerService.listPage(map));
		return result;
	}
	@RequestMapping("/list")
	public List<TbBuyer> list(){
		return buyerService.listPage(null);
	}
	@RequestMapping("/delete")
	public boolean removeBuyer(@RequestParam("bids[]")String[] bids){
		return buyerService.deleteBuyer(bids);
	}

	@RequestMapping("/update")
	public boolean updateBuyer(TbBuyer buyer) {
		LocalDateTime now = LocalDateTime.now();
		buyer.setModifyTime(now);
		return buyerService.updateBuyer(buyer);
	}
	
	@RequestMapping("/register")
	public boolean register(TbBuyer buyer) {
		//对密码进行加密
		String password = buyer.getPassword();
		String username = buyer.getUsername();
		password = new Md5Hash(password, username+password, 1000).toString();//加密
		buyer.setPassword(password);
		System.out.println(password);
		
		LocalDateTime now = LocalDateTime.now();
		buyer.setModifyTime(now);
		buyer.setCreateTime(now);
		return buyerService.insertBuyer(buyer);
	}
	
	@RequestMapping("/insert")
	public boolean insertBuyer(TbBuyer buyer) {
		LocalDateTime now = LocalDateTime.now();
		buyer.setModifyTime(now);
		buyer.setCreateTime(now);
		return buyerService.insertBuyer(buyer);
	}
	
	@RequestMapping("/login")
	public String login(String username,String password){
		//1.将登录的用户名和密码封装到shiro的令牌类中
		
		password = new Md5Hash(password, username+password, 1000).toString();//加密
		System.out.println(password);
		MyToken token = new MyToken(username,password,"Buyer");
		//2.根据抛出的异常来返回值
		Subject subject=SecurityUtils.getSubject();
		if(!subject.isAuthenticated()){
			//如果主体没有认证通过
			try {
				subject.login( token );//会跳转到自定义的MyRealm类中
			} catch ( UnknownAccountException uae ) {
				//用户名输入不正确
				System.out.println("用户名输入不正确");
				return "-1";
			} catch ( IncorrectCredentialsException ice ) {
				System.out.println("密码输入错误");
				return "-2";
			} catch (AuthenticationException e) {
				System.out.println("认证失败");
				e.printStackTrace();
				return "-3";
			}
		}
		System.out.println("登录成功");
		MyAccount account = (MyAccount)SecurityUtils.getSubject().getSession().getAttribute("account");
		if(account!=null) {
			System.out.println(account.getLoginType());
			System.out.println(account.getUsername());
		}
		else 
			System.out.println("no login");
		return "0";//登录成功
	}
}

